Centos搭建LNMP环境
1. 安装关联库
# mkdir -p /data/tgz
# cd /data/tgz
# yum install wget
# yum install pcre
# yum install openssl*
# yum install tar
# yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make gd gd2 gd-devel gd2-devel
# ulimit -SHn 65535
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
# tar zxvf pcre-8.36.tar.gz
# cd pcre-8.36
# ./configure --prefix=/data/apps/pcre
# make && make install
# cd ../
2. 安装nginx
# /usr/sbin/groupadd nginx
# /usr/sbin/useradd -g nginx nginx
# wget http://nginx.org/download/nginx-1.7.9.tar.gz
# tar zxvf nginx-1.7.9.tar.gz
# cd nginx-1.7.9
# ./configure --user=nginx --group=nginx --prefix=/data/apps/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/data/tgz/pcre-8.36 --with-http_realip_module --with-http_image_filter_module
# make && make install
# cd ../
3. 安装MySQL
# /usr/sbin/groupadd mysql
# /usr/sbin/useradd -g mysql mysql
# wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz
# tar zxvf mysql-5.6.22-linux-glibc2.5-x86_64.tar.gz
# mv mysql-5.6.22-linux-glibc2.5-x86_64 /data/apps/mysql
配置运行MySQL
1)创建数据库存放目录
# mkdir -p /data/data/mysql/data
# mkdir -p /data/data/mysql/binlog/
# mkdir -p /data/data/mysql/relaylog/
# chown -R mysql:mysql /data/data/mysql/
# yum install libaio
2)创建mysql配置文件
# vi /etc/my.cnf
输入以下内容
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user = mysql
port = 3306
socket = /tmp/mysql.sock
basedir = /data/apps/mysql
datadir = /data/data/mysql/data
log-error = /data/data/mysql/mysql_error.log
pid-file = /data/data/mysql/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
#thread_concurrency = 8
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 3
log-slave-updates
log-bin = /data/data/mysql/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /data/data/mysql/relaylog/relaylog
relay-log-info-file = /data/data/mysql/relaylog/relaylog
relay-log = /data/data/mysql/relaylog/relaylog
expire_logs_days = 30
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
#master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
server-id = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
slow-query-log-file = /data/data/mysql/slow.log
long_query_time = 10
[mysqldump]
quick
max_allowed_packet = 32M
3)以mysql用户帐号的身份建立数据表
# /data/apps/mysql/scripts/mysql_install_db --basedir=/data/apps/mysql --datadir=/data/data/mysql/data --user=mysql
# sed -i "s#/usr/local/mysql#/data/apps/mysql#g" /data/apps/mysql/bin/mysqld_safe
4)创建mysql启动,重启,停止shell脚本
# vi /data/apps/mysql/mysql
输入以下内容(这里的用户名admin和密码12345678接下来的步骤会创建)
#!/bin/sh
mysql_port=3306
mysql_username="admin"
mysql_password="12345678"
function_start_mysql()
{
printf "Starting MySQL...\n"
/bin/sh /data/apps/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
/data/apps/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 5
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
if [ "$1" = "start" ]; then
function_start_mysql
elif [ "$1" = "stop" ]; then
function_stop_mysql
elif [ "$1" = "restart" ]; then
function_restart_mysql
elif [ "$1" = "kill" ]; then
function_kill_mysql
else
printf "Usage: /data/data/mysql/mysql {start|stop|restart|kill}\n"
fi
5)赋予shell脚本可执行权限
# chmod +x /data/apps/mysql/mysql
6)启动MySQL
# /data/apps/mysql/mysql start
7)通过命令行登录管理MySQL服务器(提示输入密码时直接回车)
# /data/apps/mysql/bin/mysql -uroot -p -S /tmp/mysql.sock
8)输入以下SQL语句,创建一个具有root权限的用户(admin)和密码(12345678)
> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '12345678';
> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '12345678';
> quit
![blob.png][2]
[2]: http://www.vckai.com/static/up/image/20150101/1420523394.png ()
9)(可选)重启/停止MySQL
# /data/apps/mysql/mysql restart
# /data/apps/mysql/mysql stop
4. 编译安装PHP依赖库
# mkdir -p /data/apps/libs/
# wget http://www.ijg.org/files/jpegsrc.v9.tar.gz
# tar zxvf jpegsrc.v9.tar.gz
# cd jpeg-9/
# ./configure --prefix=/data/apps/libs --enable-shared --enable-static
# make && make install
# cd ../
# wget http://prdownloads.sourceforge.net/libpng/libpng-1.6.2.tar.gz
# tar zxvf libpng-1.6.2.tar.gz
# cd libpng-1.6.2/
# ./configure --prefix=/data/apps/libs
# make && make install
# cd ../
# wget http://download.savannah.gnu.org/releases/freetype/freetype-2.4.12.tar.gz
# tar zxvf freetype-2.4.12.tar.gz
# cd freetype-2.4.12/
# ./configure --prefix=/data/apps/libs
# make && make install
# cd ../
# wget --content-disposition "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?big_mirror=0"
# wget --content-disposition "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?big_mirror=0"
# wget --content-disposition "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?big_mirror=0"
# tar zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8/
# ./configure --prefix=/data/apps/libs
# make && make install
# cd libltdl/
# ./configure --prefix=/data/apps/libs --enable-ltdl-install
# make && make install
# cd ../../
# tar zxvf mhash-0.9.9.9.tar.gz
# cd mhash-0.9.9.9/
# ./configure --prefix=/data/apps/libs
# make && make install
# cd ../
# vi /etc/ld.so.conf
在文件末尾添加以下内容
/data/apps/libs/lib
重新加载该文件,使其即刻生效
# ldconfig
# tar zxvf mcrypt-2.6.8.tar.gz
# cd mcrypt-2.6.8/
# export LDFLAGS="-L/data/apps/libs/lib -L/usr/lib"
# export CFLAGS="-I/data/apps/libs/include -I/usr/include"
# touch malloc.h
# ./configure --prefix=/data/apps/libs --with-libmcrypt-prefix=/data/apps/libs
# make && make install
# cd ../
5. 安装PHP
# wget --content-disposition http://cn2.php.net/get/php-5.6.4.tar.gz/from/this/mirror
# tar zxvf php-5.6.4.tar.gz
# cd php-5.6.4/
# export LIBS="-lm -ltermcap -lresolv"
# export DYLD_LIBRARY_PATH="/data/apps/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
# export LD_LIBRARY_PATH="/data/apps/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
# ./configure --prefix=/data/apps/php --with-config-file-path=/data/apps/php/etc --with-mysql=/data/apps/mysql --with-mysqli=/data/apps/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir=/data/apps/libs --with-jpeg-dir=/data/apps/libs --with-png-dir=/data/apps/libs --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/data/apps/libs --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-opcache --with-pdo-mysql --enable-maintainer-zts
# make && make install
# cp php.ini-development /data/apps/php/etc/php.ini
# cd ../
6. 安装PHP扩展
# wget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
# tar zxvf autoconf-latest.tar.gz
# cd autoconf-2.69/
# ./configure --prefix=/data/apps/libs
# make && make install
# cd ../
# wget http://pecl.php.net/get/memcache-2.2.7.tgz
# tar zxvf memcache-2.2.7.tgz
# cd memcache-2.2.7/
# export PHP_AUTOCONF="/data/apps/libs/bin/autoconf"
# export PHP_AUTOHEADER="/data/apps/libs/bin/autoheader"
# /data/apps/php/bin/phpize
# ./configure --with-php-config=/data/apps/php/bin/php-config
# make && make install
# cd ../
![blob.png][3]
[3]: http://www.vckai.com/static/up/image/20150101/1420527473.png ()
1)php.ini配置文件中添加memcache扩展
# vi /data/apps/php/etc/php.ini
在文件末尾添加以下内容
extension_dir="/data/apps/php/lib/php/extensions/no-debug-zts-20131226/"
extension=memcache.so
7. 启动nginx+php
mysql的启动就不在这里多说,可参考上面。
1)调整nginx配置文件(这里只是最简单的可运行php的nginx的配置,详细的自行)**
**
# mv /data/apps/nginx/conf/nginx.conf /data/apps/nginx/conf/nginx.conf.default
# vi /data/apps/nginx/conf/nginx.conf
输入以下内容
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /data/www;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
2)创建web目录
# mkdir -p /data/www
# chown -R nginx:nginx /data/www
3)启动nginx
# /data/apps/nginx/sbin/nginx
4)启动php-fpm
# /data/apps/php/sbin/php-fpm
OK,现在已经安装完成了,可以在浏览器中输入http://localhost访问你的网站了!
坚持原创技术分享,您的支持将鼓励我继续创作!